package mynme;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

// 传感器数据接口
// 传感器数据基类
public abstract class SensorData {
    protected String sensorId;
    protected String location;
    protected LocalDateTime timestamp;

    public SensorData(String sensorId, String location) {
        this.sensorId = sensorId;
        this.location = location;
        this.timestamp = LocalDateTime.now();
    }

    public String getSensorId() { return sensorId; }
    public String getLocation() { return location; }
    public LocalDateTime getTimestamp() { return timestamp; }

    public abstract String getDataType();//数据类型
    public abstract Object getValue();//原始数据
    public abstract String getUnit();//单位（如%）

    public String getFormattedValue() {
        Object value = getValue();
        if (value instanceof Number) {
            return String.format("%.2f %s", value, getUnit());
        }
        return value.toString() + (getUnit().isEmpty() ? "" : " " + getUnit());
    }

    public void display() {
        System.out.println("=== 传感器数据 ===");
        System.out.println("传感器ID: " + sensorId);
        System.out.println("位置: " + location);
        System.out.println("时间: " + timestamp.format(DateTimeFormatter.ISO_LOCAL_TIME));
        System.out.println("类型: " + getDataType());
        System.out.println("数值: " + getFormattedValue());
    }
}
// 土壤湿度数据
class SoilMoistureData extends SensorData {
    private double moisture; // 百分比

    public SoilMoistureData(String sensorId, String location, double moisture) {
        super(sensorId, location);
        this.moisture = moisture;
    }

    @Override public String getDataType() { return "土壤湿度"; }
    @Override public Double getValue() { return moisture; }
    @Override public String getUnit() { return "%"; }
}

// 土壤pH值数据
class SoilPHData extends SensorData {
    private double phValue;

    public SoilPHData(String sensorId, String location, double phValue) {
        super(sensorId, location);
        this.phValue = phValue;
    }

    @Override public String getDataType() { return "土壤pH值"; }
    @Override public Double getValue() { return phValue; }
    @Override public String getUnit() { return "pH"; }
}

// 土壤养分数据
class SoilNutrientData extends SensorData {
    private double nitrogen; // 氮含量 mg/kg
    private double phosphorus; // 磷含量 mg/kg
    private double potassium; // 钾含量 mg/kg

    public SoilNutrientData(String sensorId, String location,
                            double nitrogen, double phosphorus, double potassium) {
        super(sensorId, location);
        this.nitrogen = nitrogen;
        this.phosphorus = phosphorus;
        this.potassium = potassium;
    }

    @Override public String getDataType() { return "土壤养分含量"; }

    @Override public String getValue() {
        return String.format("N:%.1f, P:%.1f, K:%.1f", nitrogen, phosphorus, potassium);
    }

    @Override public String getUnit() { return "mg/kg"; }

    public double getNitrogen() { return nitrogen; }
    public double getPhosphorus() { return phosphorus; }
    public double getPotassium() { return potassium; }

    @Override
    public void display() {
        super.display();
        System.out.println("氮含量(N): " + nitrogen + " mg/kg");
        System.out.println("磷含量(P): " + phosphorus + " mg/kg");
        System.out.println("钾含量(K): " + potassium + " mg/kg");
    }
}

// 气象数据
class WeatherData extends SensorData {
    private double temperature; // 摄氏度
    private double humidity;    // 百分比
    private double windSpeed;   // 米/秒

    public WeatherData(String sensorId, String location,
                       double temperature, double humidity, double windSpeed) {
        super(sensorId, location);
        this.temperature = temperature;
        this.humidity = humidity;
        this.windSpeed = windSpeed;
    }

    @Override public String getDataType() { return "气象数据"; }

    @Override public String getValue() {
        return String.format("温度:%.1f°C, 湿度:%.1f%%, 风速:%.1fm/s",
                temperature, humidity, windSpeed);
    }

    @Override public String getUnit() { return ""; }

    public double getTemperature() { return temperature; }
    public double getHumidity() { return humidity; }
    public double getWindSpeed() { return windSpeed; }

    @Override
    public void display() {
        super.display();
        System.out.println("温度: " + temperature + "°C");
        System.out.println("湿度: " + humidity + "%");
        System.out.println("风速: " + windSpeed + "m/s");
    }
}

// 作物生长数据
class CropGrowthData extends SensorData {
    private double height;      // 厘米
    private double leafArea;    // 平方厘米
    private int healthStatus;   // 1-10评分

    public CropGrowthData(String sensorId, String location,
                          double height, double leafArea, int healthStatus) {
        super(sensorId, location);
        this.height = height;
        this.leafArea = leafArea;
        this.healthStatus = healthStatus;
    }

    @Override public String getDataType() { return "作物生长状态"; }

    @Override public String getValue() {
        return String.format("高度:%.1fcm, 叶面积:%.1fcm², 健康度:%d/10",
                height, leafArea, healthStatus);
    }

    @Override public String getUnit() { return ""; }

    public double getHeight() { return height; }
    public double getLeafArea() { return leafArea; }
    public int getHealthStatus() { return healthStatus; }

    @Override
    public void display() {
        super.display();
        System.out.println("植株高度: " + height + "cm");
        System.out.println("叶面积: " + leafArea + "cm²");
        System.out.println("健康状态: " + healthStatus + "/10");

        if (healthStatus < 4) {
            System.out.println("警告: 作物健康状况不佳！");
        } else if (healthStatus < 7) {
            System.out.println("注意: 作物健康状况一般");
        } else {
            System.out.println("作物健康状况良好");
        }
    }
}
